home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / gcc / ixemlsrc.lha / ixemul / ixnet / buf.c < prev    next >
C/C++ Source or Header  |  1996-03-13  |  9KB  |  357 lines

  1. /*-
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Margo Seltzer.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char sccsid[] = "@(#)buf.c       5.7 (Berkeley) 3/12/91";
  39. #endif /* LIBC_SCCS and not lint */
  40. #undef DEBUG
  41. /******************************************************************************
  42.  
  43. PACKAGE: hash
  44.  
  45. DESCRIPTION:
  46.     Contains buffer management
  47.  
  48. ROUTINES:
  49.     External
  50.     __buf_init
  51.     __get_buf
  52.     __buf_free
  53.     __reclaim_buf
  54.     Internal
  55.     newbuf
  56.  
  57. ******************************************************************************/
  58. #define KERNEL
  59. #include "ixnet.h"
  60. #include <sys/param.h>
  61. #include <errno.h>
  62. #include <stdio.h>
  63. #include <stdlib.h>
  64. #include "hash.h"
  65. #include "utils.h"
  66.  
  67. #define assert(x) \
  68. { \
  69.     if(!x) {\
  70.     fprintf(stderr,"assertion failed on line %d, in file %s\n",__LINE__,__FILE__); \
  71.     exit(1);\
  72.     }\
  73. }
  74.  
  75. /* Externals */
  76. extern HTAB    *hashp;
  77.  
  78. /* My internals */
  79. static BUFHEAD *newbuf();
  80.  
  81. /* Unlink B from its place in the lru */
  82. #define BUF_REMOVE(B)                   \
  83. {                    \
  84.     B->prev->next = B->next;        \
  85.     B->next->prev = B->prev;        \
  86. }
  87.  
  88. /* Insert B after P */
  89. #define BUF_INSERT(B,P)                 \
  90. {                    \
  91.     B->next = P->next;            \
  92.     B->prev = P;            \
  93.     P->next = B;            \
  94.     B->next->prev = B;            \
  95. }
  96.  
  97. #define MRU    hashp->bufhead.next
  98. #define LRU    hashp->bufhead.prev
  99.  
  100. #define MRU_INSERT(B)   BUF_INSERT(B,(&hashp->bufhead))
  101. #define LRU_INSERT(B)   BUF_INSERT(B,LRU)
  102.  
  103. /*
  104.     We are looking for a buffer with address "addr".
  105.     If prev_bp is NULL, then address is a bucket index.
  106.     If prev_bp is not NULL, then it points to the page previous
  107.     to an overflow page that we are trying to find.
  108.  
  109.     CAVEAT:  The buffer header accessed via prev_bp's ovfl field
  110.     may no longer be valid.  Therefore, you must always verify that
  111.     its address matches the address you are seeking.
  112. */
  113. extern BUFHEAD *
  114. __get_buf ( addr, prev_bp, newpage )
  115. u_int    addr;
  116. BUFHEAD *prev_bp;
  117. int    newpage;        /* If prev_bp is set, indicates that this is
  118.                     a new overflow page */
  119. {
  120.     register int    segment_ndx = 0;
  121.     register    BUFHEAD *bp;
  122.     register    unsigned    is_disk = 0;
  123.     register    unsigned    is_disk_mask = 0;
  124.     SEGMENT    segp = (SEGMENT)0;
  125.  
  126.     if ( prev_bp ) {
  127.     bp = prev_bp->ovfl;
  128.     if ( !bp || (bp->addr != addr) ) bp = NULL;
  129.     if ( !newpage ) is_disk = BUF_DISK;
  130.     }
  131.     else {
  132.     /* Grab buffer out of directory */
  133.     segment_ndx = addr & ( hashp->SGSIZE - 1 );
  134.  
  135.     /*
  136.      * valid segment ensured by __call_hash()
  137.      */
  138.     segp = hashp->dir[addr >> hashp->SSHIFT];
  139. #ifdef DEBUG
  140.     assert(segp != NULL);
  141. #endif
  142.     bp = PTROF(segp[segment_ndx]);
  143.     is_disk_mask = ISDISK(segp[segment_ndx]);
  144.     is_disk = is_disk_mask || !hashp->new_file;
  145.     }
  146.  
  147.     if ( !bp ) {
  148.     bp = newbuf ( addr, prev_bp );
  149.     if ( !bp || __get_page ( bp->page, addr, !prev_bp, is_disk, 0 )) {
  150.         return(NULL);
  151.     }
  152.     if ( !prev_bp ) {
  153.         segp[segment_ndx] = (BUFHEAD *)((unsigned)bp | is_disk_mask );
  154.     }
  155.     } else {
  156.     BUF_REMOVE ( bp );
  157.     MRU_INSERT ( bp );
  158.     }
  159.     return(bp);
  160. }
  161.  
  162. /*
  163.     We need a buffer for this page. Either allocate one, or
  164.     evict a resident one (if we have as many buffers as we're
  165.     allowed) and put this one in.
  166.  
  167.     If newbuf finds an error (returning NULL), it also sets errno
  168. */
  169. static BUFHEAD *
  170. newbuf ( addr, prev_bp )
  171. u_int    addr;
  172. BUFHEAD *prev_bp;
  173. {
  174.     register    BUFHEAD *bp;    /* The buffer we're going to use */
  175.     register    BUFHEAD *xbp;    /* Temp pointer */
  176.     register    BUFHEAD *next_xbp;
  177.     int segment_ndx;
  178.     u_short    *shortp;
  179.     u_short    oaddr;
  180.     SEGMENT    segp;
  181.  
  182.     oaddr = 0;
  183.     bp = LRU;
  184.     /*
  185.     If LRU buffer is pinned, the buffer pool is too small.
  186.     We need to allocate more buffers
  187.     */
  188.     if ( hashp->nbufs || (bp->flags & BUF_PIN) ) {
  189.     /* Allocate a new one */
  190.     bp = (BUFHEAD *)malloc ( sizeof (struct _bufhead) );
  191.     if ( !bp || !(bp->page = (char *)malloc ( hashp->BSIZE )) ) {
  192.         return (NULL);
  193.     }
  194.     hashp->nbufs--;
  195.     } else {
  196.     /* Kick someone out */
  197.     BUF_REMOVE( bp );
  198.     /*
  199.         If this is an overflow page with addr 0, it's already
  200.         been flushed back in an overflow chain and initialized
  201.     */
  202.     if ( (bp->addr != 0) || (bp->flags & BUF_BUCKET) ) {
  203.         /*
  204.         Set oaddr before __put_page so that you get it
  205.         before bytes are swapped
  206.         */
  207.         shortp = (u_short *)bp->page;
  208.         if ( shortp[0] ) {
  209.         oaddr = shortp[shortp[0]-1];
  210.         }
  211.         if ( (bp->flags & BUF_MOD) &&
  212.          __put_page(bp->page, bp->addr, (int)IS_BUCKET(bp->flags), 0) ) {
  213.         return(NULL);
  214.         }
  215.         /*
  216.         Update the pointer to this page (i.e. invalidate it).
  217.  
  218.         If this is a new file (i.e. we created it at open time),
  219.         make sure that we mark pages which have been written to
  220.         disk so we retrieve them from disk later, rather than
  221.         allocating new pages.
  222.         */
  223.  
  224.         if ( IS_BUCKET(bp->flags)) {
  225.         segment_ndx = bp->addr & ( hashp->SGSIZE - 1 );
  226.  
  227.         segp = hashp->dir[bp->addr >> hashp->SSHIFT];
  228.  
  229.         assert(segp != NULL);
  230.  
  231.         if ( hashp->new_file &&
  232.              ((bp->flags & BUF_MOD) || ISDISK(segp[segment_ndx])) ) {
  233.             segp[segment_ndx] = (BUFHEAD *)BUF_DISK;
  234.         } else segp[segment_ndx] = NULL;
  235.         }
  236.  
  237.         /*
  238.         Since overflow pages can only be access by means of
  239.         their bucket, free overflow pages associated with this
  240.         bucket.
  241.         */
  242.         for ( xbp = bp; xbp->ovfl; ) {
  243.  
  244.         next_xbp = xbp->ovfl;
  245.         xbp->ovfl = 0;
  246.         xbp = next_xbp;
  247.  
  248.         /* Check that ovfl pointer is up date */
  249.         if ( IS_BUCKET(xbp->flags) || (oaddr != xbp->addr) ) break;
  250.  
  251.         shortp = (u_short *)xbp->page;
  252.         if ( shortp[0] ) {
  253.             oaddr = shortp[shortp[0]-1];  /* set before __put_page */
  254.         }
  255.         if ( (xbp->flags & BUF_MOD) &&
  256.             __put_page ( xbp->page, xbp->addr, 0, 0 ) ) {
  257.             return(NULL);
  258.         }
  259.         xbp->addr = 0;
  260.         xbp->flags = 0;
  261.         BUF_REMOVE ( xbp );
  262.         LRU_INSERT ( xbp );
  263.         }
  264.     }
  265.     }
  266.  
  267.     /* Now assign this buffer */
  268.     bp->addr = addr;
  269. #ifdef DEBUG1
  270.     fprintf ( stderr, "NEWBUF1: %d->ovfl was %d is now %d\n", bp->addr,
  271.         (bp->ovfl?bp->ovfl->addr:0),  0);
  272. #endif
  273.     bp->ovfl = NULL;
  274.     if ( prev_bp ) {
  275.     /*
  276.         If prev_bp is set, this is an overflow page, hook it in to the
  277.         buffer overflow links
  278.     */
  279. #ifdef DEBUG1
  280.     fprintf ( stderr, "NEWBUF2: %d->ovfl was %d is now %d\n", prev_bp->addr,
  281.             (prev_bp->ovfl?bp->ovfl->addr:0),
  282.             (bp?bp->addr: 0));
  283. #endif
  284.     prev_bp->ovfl = bp;
  285.     bp->flags = 0;
  286.     } else bp->flags = BUF_BUCKET;
  287.     MRU_INSERT ( bp );
  288.     return ( bp );
  289. }
  290.  
  291. extern void
  292. __buf_init ( nbytes )
  293. int    nbytes;
  294. {
  295.     int npages;
  296.     BUFHEAD    *bfp = &(hashp->bufhead);
  297.  
  298.     npages = (nbytes + hashp->BSIZE - 1) >> hashp->BSHIFT;
  299.     npages = MAX ( npages, MIN_BUFFERS );
  300.  
  301.     hashp->nbufs = npages;
  302.     bfp->next = bfp;
  303.     bfp->prev = bfp;
  304.     /*
  305.     This space is calloc'd so these are already null
  306.  
  307.     bfp->ovfl = NULL;
  308.     bfp->flags = 0;
  309.     bfp->page = NULL;
  310.     bfp->addr = 0;
  311.     */
  312. }
  313.  
  314. extern int
  315. __buf_free ( do_free, to_disk )
  316. int    do_free;
  317. int    to_disk;
  318. {
  319.     BUFHEAD    *bp;
  320.  
  321.     /* Need to make sure that buffer manager has been initialized */
  322.     if ( !LRU ) {
  323.     return(0);
  324.     }
  325.  
  326.     for ( bp = LRU; bp != &hashp->bufhead; ) {
  327.     /* Check that the buffer is valid */
  328.     if ( bp->addr || IS_BUCKET(bp->flags) ) {
  329.         if ( to_disk && (bp->flags & BUF_MOD) &&
  330.          __put_page (bp->page, bp->addr, IS_BUCKET(bp->flags), 0 )) {
  331.         return (-1);
  332.         }
  333.     }
  334.  
  335.     /* Check if we are freeing stuff */
  336.     if ( do_free ) {
  337.         if ( bp->page ) free ( bp->page );
  338.         BUF_REMOVE(bp);
  339.         (void)free ( bp );
  340.         bp = LRU;
  341.     } else bp = bp->prev;
  342.     }
  343.  
  344.     return(0);
  345. }
  346.  
  347. extern void
  348. __reclaim_buf ( bp )
  349. BUFHEAD *bp;
  350. {
  351.     bp->ovfl = 0;
  352.     bp->addr = 0;
  353.     bp->flags = 0;
  354.     BUF_REMOVE ( bp );
  355.     LRU_INSERT ( bp );
  356. }
  357.